package com.nanbei.redblack;

/**
 * @ClassDescription: 红黑树节点
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/6/5 16:39
 */
public class RedBlackNode {
    int key;
    Object value;
    RedBlackNode right;
    RedBlackNode left;
    RedBlackNode parent; // 父节点
    Color color = Color.RED;

    public RedBlackNode(int key) {
        this.key = key;
    }

    public RedBlackNode(int key, Object value) {
        this.key = key;
        this.value = value;
    }

    public RedBlackNode(int key, Color color) {
        this.key = key;
        this.color = color;
    }

    public RedBlackNode(int key, Object value, RedBlackNode left, RedBlackNode right) {
        this.key = key;
        this.value = value;
        this.right = right;
        this.left = left;
        if (left != null) {
            left.parent = this;
        }
        if (right != null) {
            right.parent = this;
        }
    }

    public RedBlackNode(int key, Color color, RedBlackNode left, RedBlackNode right) {
        this.key = key;
        this.color = color;
        this.right = right;
        this.left = left;
        if (left != null) {
            left.parent = this;
        }
        if (right != null) {
            right.parent = this;
        }
    }

    public boolean isLeftChild(){
        return parent != null && parent.left == this;
    }

    // 叔叔节点
    public RedBlackNode uncle() {
        // 没有父节点，则是根节点，则没有叔叔节点 。如果没有爷爷节点则没有叔叔节点
        if (parent == null || parent.parent == null) {
            return null;
        }
        if (parent.isLeftChild()) {
            return parent.parent.right ;
        } else {
            return parent.parent.left ;
        }
    }

    // 兄弟节点
    public RedBlackNode sibling(){
        // 如果没有父节点，这没有兄弟节点。
        if (parent == null ) {
            return null;
        }
        if (this.isLeftChild()) {
            return parent.right ;
        } else {
            return parent.left ;
        }

    }


}
